package controller;

import dao.impl.CartDAOImpl;
import dao.impl.GoodsDAOImpl;
import model.Goods;
import model.Trolley;
import model.common.Msg;
import service.CartService;
import service.impl.CartServiceImpl;
import utils.JSONUtil;
import view.Client;

import java.util.*;


public class CartController extends BaseController{
    private Goods goods;
    private static CartDAOImpl cartDAO;
    private static CartService cartService;
    private static String userid;
    private static List<Trolley> trolleyList;
    private Scanner input;
    public CartController() {
        cartService = new CartServiceImpl();
        cartDAO = new CartDAOImpl();
        input = new Scanner(System.in);

    }


    private static Map<String, Integer> cart = new HashMap<>();


    public String addCart(Goods goods){
        Trolley trolley = new Trolley();
        userid = new UserController().getLoginUser().getId();
        trolley.setUserId(userid);
        trolley.setGoodsId(goods.getId());
        trolley.setCreateTime(new Date());
        trolley.setId(getTrolleyList());

        System.out.println("请输入要添加的商品数量：");
        Integer goodsNumber = input.nextInt();
        trolley.setGoodsNum(goodsNumber);
        trolley.setIsDel("1");

        Msg msg = new Msg();
        try {
            int result = cartService.addTrolley(trolley);
            if (result > 0){
                msg.setType (Msg.SUCCESS);
                System.out.println("商品:"+goods.getName()+" 添加购物成功！数量为:"+trolley.getGoodsNum());
            }
            else
                throw new Exception();
        }
        catch (Exception e) {
            msg.setType(Msg.FAIL);
            msg.setMsg("服务器异常");
        }
        return JSONUtil.entity2JSON(msg);
    }

    //获得购物车
    public static Map<String,Integer> getCart(){
        userid = new UserController().getLoginUser().getId(); // userid测试用 183957aadac74d6eaf5653dc17cb0e22
        cart = new HashMap<>();
        trolleyList = cartService.getTrolleyById(userid);
        for (Trolley item: trolleyList) {
            cart.put(item.getGoodsId(),item.getGoodsNum());
        }
        return cart;
    }



    //展示购物车
    public void showCart(){
        if (cart == null || cart.isEmpty()) getCart();
        GoodsDAOImpl goodsDAO = new GoodsDAOImpl();
        int index = 1;
        System.out.println("-------------购物车列表-----------------------");
        System.out.println("编号\t\t" + "商品名称" + "\t\t" + "单价" +"\t\t" + "数量"+"\t\t" + "品牌");
        for (Map.Entry<String, Integer> entry : cart.entrySet()) {
            System.out.println(index++ +"\t\t" + goodsDAO.getGoodsById(entry.getKey()).getName() + "\t\t\t" +
                    goodsDAO.getGoodsById(entry.getKey()).getPrice() + "\t\t" +
                    entry.getValue() + "\t\t" +
                    goodsDAO.getGoodsById(entry.getKey()).getBrand());
        }
    }

    //更新购物车
    public String updateOrder() {
        Trolley trolley = new Trolley();
        int changeNumber = -1;
        showCart();
        System.out.println("请输入要更改的商品的编号：");
        int number = input.nextInt();
        if (number <= 0 || number > cart.size()) {
            System.out.println("编号不存在");
            return Client.CART;
        }
        else {
            int index = 1;
            for (Map.Entry<String, Integer> entry : cart.entrySet()){
                if (number == index){
                    trolley = trolleyList.get(index-1);
                    break;
                }
                index++;
            }
            System.out.println("请输入要修改的数量：");
            changeNumber = input.nextInt();
        }


        if (changeNumber < 0) {
            System.out.println("要购买的商品数量不正确！！！");
            return Client.CART;
        }
        trolley.setGoodsNum(changeNumber);
        cartService.updateTrolley(trolley);
        System.out.println("修改商品数量成功！");
        getCart();
        return Client.CART;

    }

    // 购物车
    public String payCart(){

        return Client.PAY;
    }

    // 删除购物车
    public void deleteCart(){
        showCart();
        System.out.println("请输入要删除的商品编号，或者 A 全部删除");
        String option = input.next();
        if ("a".equals(option)){
            // 全部删除
            cartService.deleteTrolleyAll(userid);
            System.out.println("购物车全部删除成功！");
            getCart();
            return;
        }

        if (!Character.isDigit(option.toCharArray()[0])){
            System.out.println("输入的不是数字，检查后重拾");
            deleteCart();
            return;
        }
        int number = Integer.parseInt(option);
        if (number <= 0 || number > cart.size())
            System.out.println("编号不存在");
        else {
            int index = 1;
            String goodsid = "";
            // 单个删除
            for (Map.Entry<String, Integer> entry : cart.entrySet()){
                if (number == index){
                    goodsid = entry.getKey();
                    break;
                }
                index++;
            }
            cartService.deleteTrolley(userid, goodsid);
            System.out.println("删除" + new GoodsDAOImpl().getGoodsById(goodsid).getName()+"成功！");
        }
        // 删除完成后重新获取购物车
        getCart();
    }

    // 获取购物车表长度
    private String getTrolleyList(){
        return (cartService.getAllTrolley().size()+1) + "";
    }

}
